var express = require('express');
var router = express.Router();
var reqUtil = require('../lib/req');
var LINK = require('../api/link');

/* GET users listing. */
router.get('/', function(req, res) {
    res.send('respond with a resource');
});

router.get('/detail/:id', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        var id = req.params.id;
        var url = LINK.REQ_GET_ORDER_INFO;

        reqUtil.getData(url + id, function(code, data) {
            if (!code) {
                var user = false;
                if (req.session) {
                    if (req.session.user) {
                        data.user = true;
                    }
                };
                res.render('order/content', data);
            } else {
                res.redirect('/');
            }
        });
    } else {
        res.render('login', { link: req.originalUrl });
    }

    // var obj = {
    //     order: {
    //         id: '1',
    //         orderId: '19246500',
    //         state: '7',
    //         addTime: '2016-05-30 10:11:17',
    //         payTime: '2016-05-30 10:11:17'
    //     },
    //     course: [
    //         { id: '1', title: '走进电商走近淘宝', img: '/images/310.png', teacher: '教师名字（取前2位）', type: '1', times: '10', price: '100' },
    //         { id: '1', title: '走进电商走近淘宝', img: '/images/310.png', teacher: '教师名字（取前2位）', type: '1', times: '10', price: '100' }
    //     ]
    // };
    // res.render('order/content', obj);
});


router.post('/submit', function(req, res) {
    var ids = req.body.ids;
    console.log(ids);
    var obj = {};
    res.json({
        code: 0,
        id: '2'
    });
});

router.get('/pay/:id', function(req, res) {
    var id = req.params.id;
    var obj = {};
    res.render('order/pay', { id: id });
});

router.get('/apply/:id', function(req, res) {
    var id = req.params.id;
    var obj = {};
    res.render('order/apply', { id: id });
});

router.get('/remit/:id', function(req, res) {
    var id = req.params.id;
    var obj = {};
    res.render('order/remit', { id: id });
});

module.exports = router;